﻿using System;
using System.Collections.Generic;
using System.Text;
using Xeno.Core;
using OpenTK;
using OpenTK.Graphics.OpenGL;


namespace Xeno.GUI
{
    public class XuiGraphSelector : XuiWidget
    {
        public class NodeState
        {
            public bool Open { get; set; }
            public Vector3 Col = new Vector3(0, 0, 0);
            public XenoText.XenoTextCache TextCache;
        }
        public XenoGraph Graph { get; set; }
        public Dictionary<XenoNode, NodeState> Nodes = new Dictionary<XenoNode, NodeState>();
        public XuiGraphSelector(Vector2 size, XenoGraph graph)
            : base(size)
        {

            Graph = graph;

        }

       
        internal float mx=0,my=0;
        internal bool mb=false;

        internal bool click =false;
        internal bool hit = false;
       

        public override void DoRender()
        {
            Vector3 wp = Attached.WorldPosition;
            mx = XenoInput.MouseX();
            my = XenoInput.MouseY();
            mb = XenoInput.IsMouseDown(OpenTK.Input.MouseButton.Left);
            if (mb == true && click == false)
            {
                click = true;
                hit = true;
            }
            else if (mb == true)
            {
                hit = false;
            }
            else
            {
                hit = false;
                click = false;
            }
            GL.Enable(EnableCap.ScissorTest);
            GL.Scissor((int)wp.X,(int)XenoCore.DisplayHeight-(int) wp.Y-(int)Size.Y, (int)Size.X,(int) Size.Y);

            ScrollY-=XenoApp.Instance.Mouse.WheelDelta*10;
            if (ScrollY < 0) ScrollY = 0;

            RenderNode(Graph.RootNode, wp.X + 10, wp.Y + 10-ScrollY);
            GL.Disable(EnableCap.ScissorTest);
        }
        public float ScrollY = 0;

        public float RenderNode(XenoNode node, float x, float y)
        {
            
              NodeState ns = null;
            if (Nodes.ContainsKey(node))
            {
                ns = Nodes[node];
            }
            else
            {
                ns = new NodeState();
                Nodes[node] = ns;
            }

            if (ns.TextCache == null)
            {
                ns.TextCache = XenoText.CreateText(node.Name);
            }


            XenoText.DrawText(ns.TextCache,(int)x,(int)y,System.Drawing.Color.Chocolate);
            if (mx >= x && my >= y && mx <= x+ns.TextCache.Width && my <= y + 15)
            {
                Action(this,0,node as object,0,0);


            }
            float vx = x += 16;
            float vy = y += 12;
            if (mx >= vx-25 && my >= vy-25 && mx <= vx -5 && my <= vy)
            {
                //  XenoDebug.LogError("!!", "");
                if (hit)
                {

                    if (ns.Open)
                    {
                        ns.Open = false;
                    }
                    else
                    {
                        ns.Open = true;
                    }
                }
            }

            if (ns.Open)
            {
                float dx = x + 10;
                float dy = y + 25;
                foreach (var n in node.Nodes)
                {
                    dy=RenderNode(n, dx, dy);
                    dy += 20;
                }
                y = dy;
            }
            else
            {
             
                Begin2D();
                GL.Color4(System.Drawing.Color.Black);
                GL.Begin(BeginMode.Lines);

                GL.Vertex2(vx - 25, vy);
                GL.Vertex2(vx-20, vy - 5);
                GL.Vertex2(vx-20, vy - 5);
                GL.Vertex2(vx - 15, vy);
                GL.End();


                End2D();
            }
            return y;            
        }
    }

}
